package com.http;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.apache.commons.io.IOUtils;

import com.caigen.b.l;

import net.sf.json.JSONObject;

public class HttpHelper {

	JFrame jf=new JFrame("登录");
	JLabel jlName=new JLabel("登录名");
	JTextField jtName=new JTextField("superuser");
	JLabel jlPassword=new JLabel("密码");
	JPasswordField jtPassword=new JPasswordField("demo112233");
	JButton jButton=new JButton("登录");
	JButton jCancel=new JButton("取消");
	public HttpHelper() {
		jf.setBounds((int)jf.getToolkit().getScreenSize().width/3, jf.getToolkit().getScreenSize().height/4, 350, 250);
		jlName.setBounds(20,20,40,20);
		jtName.setBounds(80, 20,200,20);
		jlPassword.setBounds(20,60,40,20);
		jtPassword.setBounds(80,60, 200, 20);
		jButton.setBounds(80, 100, 80, 35);
		jCancel.setBounds(180, 100, 80, 35);
		JPanel jp=new JPanel(null);
		jp.setBounds(20,20,240,100);
		jp.setSize(240,100);
		jp.setBackground(Color.GRAY);
		jp.add(jlName);
		jp.add(jtName);
		jp.add(jlPassword);
		jp.add(jtPassword);
		jp.add(jButton);
		jp.add(jCancel);
		jf.add(jp);
		jf.setVisible(true);
		jButton.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode()==10 || e.getKeyCode()==32){
					System.out.println("e.getKeyCode()="+e.getKeyCode());
					dologin();
				}
			}
		});
		jButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				System.out.println("e.getClickCount="+e.getClickCount());
				dologin();
			}
		});
		jCancel.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				System.exit(1);
			}
		});
		jCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				System.exit(1);
			}
		});
	}
	private boolean dologin(){
		String userName=this.jtName.getText();
		String password=new String(this.jtPassword.getPassword());
		try {
			URL url=new URL("http://127.0.0.1:8080/hqcm/doLoginAction?Action=doLogin&loginId="+userName+"&password="+password+"&loginType=1&LangID=zh_CN");
			System.out.println("url="+url.toString());
			HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
		/*	urlConnection.setDoOutput(true);
			urlConnection.setDoInput(true);*/
			urlConnection.connect();
			InputStream is=urlConnection.getInputStream();
			StringBuffer sb=new StringBuffer();
			BufferedReader br=new BufferedReader(new InputStreamReader(is));
			String s=null;
			while(br!=null && (s=br.readLine())!=null){
				sb.append(s);
			}
			System.out.println("sb="+sb);
			System.out.println("obj="+JSONObject.fromObject(urlConnection.getContent()));
			is.close();
			br.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
			
		return false;
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HttpHelper hh=new HttpHelper();
		//JSONObject obj=JSONObject.fromObject(new String("{success:{indexUrl:'/index2.jsp#/jcsj/sjtb',remain:'-1'}}"));
		//System.out.println("obj="+obj);
	}

}
